package com.ruoyi.project.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.domain.SysCodeRules;
import com.ruoyi.project.system.mapper.SysCodeRulesMapper;
import com.ruoyi.project.system.service.ISysCodeRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
 * 自动编码规则Service业务层处理
 * 
 * @author woniu
 * @date 2020-07-01
 */
@Service
public class SysCodeRulesServiceImpl implements ISysCodeRulesService 
{
    @Autowired
    private SysCodeRulesMapper sysCodeRulesMapper;

    /**
     * 查询自动编码规则
     * 
     * @param id 自动编码规则ID
     * @return 自动编码规则
     */
    @Override
    public SysCodeRules selectSysCodeRulesById(Long id)
    {
        return sysCodeRulesMapper.selectSysCodeRulesById(id);
    }

    /**
     * 查询自动编码规则列表
     * 
     * @param sysCodeRules 自动编码规则
     * @return 自动编码规则
     */
    @Override
    public List<SysCodeRules> selectSysCodeRulesList(SysCodeRules sysCodeRules)
    {
        return sysCodeRulesMapper.selectSysCodeRulesList(sysCodeRules);
    }

    /**
     * 新增自动编码规则
     *
     * @param sysCodeRules 自动编码规则
     * @return 结果
     */
    @Override
    public int insertSysCodeRules(SysCodeRules sysCodeRules) {
        sysCodeRules.setCreateBy(SecurityUtils.getUserID() + "");
        sysCodeRules.setCompanyId(SecurityUtils.getUserCompanyId());
        sysCodeRules.setCreateTime(DateUtils.getNowDate());
        return sysCodeRulesMapper.insertSysCodeRules(sysCodeRules);
    }

    /**
     * 修改自动编码规则
     * 
     * @param sysCodeRules 自动编码规则
     * @return 结果
     */
    @Override
    public int updateSysCodeRules(SysCodeRules sysCodeRules)
    {
        sysCodeRules.setUpdateTime(DateUtils.getNowDate());
        return sysCodeRulesMapper.updateSysCodeRules(sysCodeRules);
    }

    /**
     * 批量删除自动编码规则
     * 
     * @param ids 需要删除的自动编码规则ID
     * @return 结果
     */
    @Override
    public int deleteSysCodeRulesByIds(Long[] ids)
    {
        return sysCodeRulesMapper.deleteSysCodeRulesByIds(ids);
    }

    /**
     * 删除自动编码规则信息
     *
     * @param id 自动编码规则ID
     * @return 结果
     */
    @Override
    public int deleteSysCodeRulesById(Long id) {
        return sysCodeRulesMapper.deleteSysCodeRulesById(id);
    }

    /**
     * 获取编码
     *
     * @param code
     * @param companyId
     * @return
     */
    @Override
    public String selectRulesByCode(String code, String companyId) {
        StringBuffer xh = new StringBuffer();
        SimpleDateFormat sdf = null;
        Calendar cal = Calendar.getInstance(Locale.CHINA);
        Calendar lastUpdateTime = Calendar.getInstance(Locale.CHINA);
        SysCodeRules rules = sysCodeRulesMapper.selectSysCodeRulesByCode(code, companyId);
        Integer curXh = rules.getDqxh() + 1;//当前序号
        String prefix = rules.getPrefix();//前缀
        String rqgs = rules.getRqgs();//日期格式
        Integer xhLength = rules.getXhLength();//几位填充
        String rq = new SimpleDateFormat(rqgs).format(cal.getTime());
        if (rules.getUpdateTime() != null) {
            lastUpdateTime.setTime(rules.getUpdateTime());
        }

        if (StringUtils.isNotEmpty(prefix)) {
            xh.append(prefix);
        }

        if (StringUtils.isNotEmpty(rqgs)) {

            sdf = new SimpleDateFormat(rqgs);
            xh.append(sdf.format(cal.getTime()));
            //更新频次
            switch (rules.getSfmrgx()) {
                case 0:
                    break;
                case 1://每天更新
                    if (sdf.format(lastUpdateTime).equals(sdf.format(cal.getTime()))) break;
                case 2://每月
                    if (lastUpdateTime.get(Calendar.MONTH) == cal.get(Calendar.MONTH)) break;
                case 3://每年
                    if (lastUpdateTime.get(Calendar.YEAR) == cal.get(Calendar.YEAR)) break;
                default:
                    curXh = 1;
            }
        }
        xh.append(StringUtils.leftPad(curXh + "", xhLength, "0"));
        rules.setUpdateTime(cal.getTime());
        rules.setDqxh(curXh);
        sysCodeRulesMapper.updateSysCodeRules(rules);
        return xh.toString();
    }

}
